#ifndef LOG_H
#define LOG_H

#include <stdio.h>
#include <pthread>
#include <string>
#include "./blockqueue.h"
#include "../lock/threadlocker.h"
using namespace std;


class log
{
private:
    //路径名
    char dir_name[128];
    //日志文件名
    char log_name[128];
    //最大行数
    int m_split_lines;
    //缓冲区大小
    int m_log_buf_size;
    //行数记录
    long long m_count; 
    //按天分类 记录哪一天
    int m_today;
    //日志文件指针
    FILE *log_fp;
    char *m_buf;
    BlockQueue<string> *m_log_queue;
    bool m_is_async;
    threadlocker m_mutex;
    //关闭日志
    int m_close_log;

public:
    log(/* args */);
    virtual ~log();
    static Log *get_instance();
    void *async_write_log();
    static void *flush_log_thread(void *args);
    bool init(const char * file_name,int close_log,int log_buf_size = 8192, int split_lines = 5000000,int max_queue_size = 0);
    void write_log(int level,const char* format,...);
    void flush(void);

}
#define LOG_DEBUG(format,...) if(m_close_log==0) {log::get_instance()->write_log(0,format,##__VA_ARGS__);log::get_instance()->flush();}
#define LOG_INFO(format,...) if(m_close_log==0) {log::get_instance()->write_log(1,format,##__VA_ARGS__);log::get_instance()->flush();}
#define LOG_WARN(format,...) if(m_close_log==0) {log::get_instance()->write_log(2,format,##__VA_ARGS__);log::get_instance()->flush();}
#define LOG_ERROR(format,...) if(m_close_log==0) {log::get_instance()->write_log(3,format,##__VA_ARGS__);log::get_instance()->flush();}

#endif